//
//  XQCUnlockView.m
//  XQC
//
//  Created by lee on 2019/5/8.
//  Copyright © 2019 xqc. All rights reserved.
//

#import "XQCUnlockView.h"
#import <FuliBaseLibraryFramework/FuliBaseLibraryFramework.h>
#import "HuangwwPCLockLabel.h"
#import "Masonry.h"
#import "HuangwwFaceAndGestureHeader.h"
#import "HuangwwFaceAndGestureTool.h"
#import "SDAutoLayout.h"
#import "HuangwwPCCircleView.h"
#import "HuangwwFaceAndGestureHeader.h"
#import "HuangwwPCCircleViewConst.h"
@implementation XQCUnlockView
{
    NSInteger _count;
    HuangwwPCLockLabel *_msgLabel;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = WhiteColor;
        self.userInteractionEnabled = YES;
        _count = kGetUserDefaults(kGestureCount) != nil ? [kGetUserDefaults(kGestureCount) integerValue] : 5;
        [self setUI];
    }
    return self;
}

- (void)setUI {
    HeaderImageView *header = [HuangwwFaceAndGestureToolManager getUserHeaderImageWithSize:CGSizeMake(60, 60) Vsize:CGSizeMake(18, 18)];
    [self addSubview:header];
    header.sd_layout
    .topSpaceToView(self, 97)
    .centerXEqualToView(self)
    .widthIs(60)
    .heightIs(60);

    
    UILabel *phone = [[UILabel alloc] init];
    [self addSubview:phone];
    [phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(header.mas_bottom).equalTo(@(19));
        make.centerX.equalTo(self.mas_centerX);
        make.height.equalTo(@(11));
    }];
    phone.text = [NSString fuliReturnPhoneStr:@"13070924231"];
    phone.textColor = kGlobalMainTextColor;
    phone.font = FULI_REGULAR_FONT_14;
    
    [self LoginTypeShow] ? [self gestureView] : [self showIDView];

    UIButton *other = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self addSubview:other];
    [other mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-(TAB_SAFE_HEIGHT+40)));
        make.centerX.equalTo(self.mas_centerX);
        make.height.equalTo(@(13));
    }];
    [other setTitle:[self LoginTypeShow] ? @"忘记密码" : @"其他登录方式" forState:(UIControlStateNormal)];
    other.titleLabel.font = FULI_REGULAR_FONT_13;
    [other setTitleColor:kGlobalWarnColor forState:(UIControlStateNormal)];
    @weakify(self);
    [[other rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.other) {
            self.other(x);
        }
    }];
    
}

- (void)showIDView {
    [self IdView];
    [self showIDVerify];
}

- (void)IdView {
    //    (title, font, maxWidth, offset, textColor)
    FULIButton *idBtn = [[FULIButton alloc] initWithCustom:kFULICustomButtonTopImage paramter:@{RACTuplePack(iPhoneX ? @"点击进行面容解锁" : @"点击进行指纹解锁",FULI_REGULAR_FONT_14,@(kPHONE_WIDTH),@(17),kGlobalMainTextColor) : RACTuplePack(iPhoneX ? @"face" : @"touchid")} clickedOnNext:^(UIGestureRecognizer * _Nonnull gesture, FULIButton * _Nonnull fuliButton) {
        [[HuangwwAuthID validateLoginAuthID] subscribeNext:^(id  _Nullable x) {
            [[RACScheduler mainThreadScheduler] schedule:^{
                [HuangwwFaceAndGestureTool hidenUnlockView];
            }];
        } error:^(NSError * _Nullable error) {
            
        }];
    }];
    [self addSubview:idBtn];
    idBtn.sd_layout
    .topSpaceToView(self, 230)
    .centerXEqualToView(self)
    .widthIs(111)
    .heightIs(111);
}

- (void)gestureView {
    HuangwwPCCircleView *lockView = [[HuangwwPCCircleView alloc] init];
    lockView.delegate = self;
    [lockView setType:CircleViewTypeVerify];
    [self addSubview:lockView];
    [lockView setY:215];
    
    UILabel *content = [[UILabel alloc] init];
    [self addSubview:content];
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lockView.mas_bottom).equalTo(@(0));
        make.centerX.equalTo(self.mas_centerX);
        make.height.equalTo(@(13));
    }];
    content.text = @"请输入手势密码";
    content.textColor = kGlobalMainTextColor;
    content.font = FULI_REGULAR_FONT_14;
    
    _msgLabel = [[HuangwwPCLockLabel alloc] init];
    [self addSubview:_msgLabel];
    [_msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lockView.mas_top).equalTo(@(-0));
        make.centerX.equalTo(self.mas_centerX);
    }];
}

#pragma mark - login or verify gesture

- (void)circleView:(HuangwwPCCircleView *)view type:(CircleViewType)type connectCirclesLessThanNeedWithGesture:(NSString *)gesture {
    [_msgLabel showWarnMsgAndShake:@"至少连线4个"];
}


- (void)circleView:(HuangwwPCCircleView *)view type:(CircleViewType)type didCompleteLoginGesture:(NSString *)gesture result:(BOOL)equal
{
    _count --;
    kSetUserDefaults(@(_count), kGestureCount);
    if (type == CircleViewTypeVerify) {
        if (equal) {
            kRemoveUserDefaults(kGestureCount);
            [HuangwwFaceAndGestureTool hidenUnlockView];
        } else {
            _count < 0 ? @"" : [_msgLabel showWarnMsgAndShake:[NSString stringWithFormat:@"密码错误，还可以输入%ld次",(long)_count]];
            if (_count <= 0) {
//                [[XQCLoginViewModel gesturePassword:@"" gestureSwitch:0 isDisplayTrajectory:[kGetUserDefaults(kShowLine) intValue]] subscribeNext:^(id  _Nullable x) {
//                    
//                }];
                kRemoveUserDefaults(kGestureCount);
                [HuangwwAuthID RemoveGesture];
                kRemoveUserDefaults(gestureFinalSaveKey);
//                [UserManager deleteAccount];
//                [XQCLoginManager showLoginViewSuccessBlock:^{
//
//                }];
                [HuangwwFaceAndGestureTool hidenUnlockView];
            }
        }
    }
}

- (void)showIDVerify {
    [[HuangwwAuthID validateLoginAuthID] subscribeNext:^(id  _Nullable x) {
        [[RACScheduler mainThreadScheduler] schedule:^{
            [HuangwwFaceAndGestureTool hidenUnlockView];
        }];
    } error:^(NSError * _Nullable error) {
        
    }];
}


- (BOOL)LoginTypeShow {
    NSString *type = kGetUserDefaults(kLockType);
    if (type) {
        if (type.intValue == 1) {
            return YES;
        }else {
            return  NO;
        }
    }else {
        if ([kGetUserDefaults(kGestureOpen) boolValue]) {
            return YES;
        }
        if (kGetUserDefaults(kAuthIDopen)) {
            return NO;
        }
    }
    return YES;
}
@end
